package com.xxx.common.core.log;

import org.apache.commons.logging.Log;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * Created by lowkey on 16/8/1.
 */
public class AppLog implements Log{

    private Log log ;
    public AppLog(Log log){
        this.log = log;
    }

    @Override
    public boolean isDebugEnabled() {
        return log.isDebugEnabled();
    }

    @Override
    public boolean isErrorEnabled() {
        return log.isErrorEnabled();
    }

    @Override
    public boolean isFatalEnabled() {
        return log.isFatalEnabled();
    }

    @Override
    public boolean isInfoEnabled() {
        return log.isInfoEnabled();
    }

    @Override
    public boolean isTraceEnabled() {
        return log.isTraceEnabled();
    }

    @Override
    public boolean isWarnEnabled() {
        return log.isWarnEnabled();
    }

    @Override
    public void trace(Object o) {
        log.trace(o);
    }

    @Override
    public void trace(Object o, Throwable throwable) {
        log.trace(getExpMsg(o.toString(),throwable));
    }

    @Override
    public void debug(Object o) {
        log.debug(o);
    }

    @Override
    public void debug(Object o, Throwable throwable) {
        log.debug(getExpMsg(o.toString(),throwable));
    }

    @Override
    public void info(Object o) {
        log.info(o);
    }

    @Override
    public void info(Object o, Throwable throwable) {
        log.info(getExpMsg(o.toString(),throwable));
    }

    @Override
    public void warn(Object o) {
        log.warn(o);
    }

    @Override
    public void warn(Object o, Throwable throwable) {
        log.warn(getExpMsg(o.toString(),throwable));
    }

    @Override
    public void error(Object o) {
        log.error(o);
    }

    @Override
    public void error(Object o, Throwable throwable) {
        log.error(getExpMsg(o.toString(),throwable));
    }

    @Override
    public void fatal(Object o) {
        log.fatal(o);
    }

    @Override
    public void fatal(Object o, Throwable throwable) {
        log.fatal(getExpMsg(o.toString(),throwable));
    }

    public static String getExpMsg(String msg,Throwable t){
        return String.format("[%s][%s]",msg,getTrace(t));
    }

    public static String getTrace(Throwable t) {
        StringWriter stringWriter= new StringWriter();
        PrintWriter writer= new PrintWriter(stringWriter);
        t.printStackTrace(writer);
        StringBuffer buffer= stringWriter.getBuffer();
        return buffer.toString();
    }
}
